package com.application.lucky.presenter;

import com.application.lucky.Lucky;
import com.application.lucky.bean.jsonBean.FriendApplyBean;
import com.application.lucky.bean.jsonBean.FriendSearchBean;
import com.application.lucky.request.CallBackEx;
import com.application.lucky.request.FriendRequest;
import com.application.lucky.view.NewFriendsView;
import com.application.mylibrary.base.mvp.IBasePresenter;
import com.baselib.libnetwork.net.libnetworkcomponent.BaseResponse;

import java.util.ArrayList;
import java.util.List;

public class NewFriendsPresenter extends IBasePresenter<NewFriendsView> {
    private final FriendRequest request;
    private int page = 0;

    public NewFriendsPresenter() {
        request = new FriendRequest();
    }

    public void loadData() {
        request.friend_apply_list(page + "", new CallBackEx<BaseResponse<FriendApplyBean>>() {

            @Override
            public void onSuccess(BaseResponse<FriendApplyBean> stringBaseResponse) {
                FriendApplyBean dataInfo = stringBaseResponse.dataInfo;
                if (getView() != null) {
                    if (dataInfo != null) {
                        List<FriendApplyBean.FriendListBean> friendList = dataInfo.getFriendList();
                        List<FriendApplyBean.FriendListBean> dbList = loadLocalData();
                        if (friendList != null) {
                            friendList.addAll(dbList);
                            getView().setNewData(friendList);
                        } else {
                            getView().setNewData(dbList);
                        }
                    }
                }
            }

            @Override
            protected void OnFailureNeedData(int code, BaseResponse<FriendApplyBean> stringBaseResponse) {

            }

            @Override
            public void onFailed(int code) {

            }
        });
    }

    private List<FriendApplyBean.FriendListBean> loadLocalData() {
        List<FriendSearchBean> list = Lucky.getApplication().getDaoSession().getFriendSearchBeanDao().loadAll();
        List<FriendApplyBean.FriendListBean> tempList = new ArrayList<>();
        for (FriendSearchBean item : list) {
            tempList.add(item.getUIBean());
        }
        return tempList;
    }
}
